package servlets;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import bdd.beans.DBLink;
import bdd.beans.Enseignant;
import bdd.beans.ListeFicheVoeux;
import bdd.daos.impl.DAOEnseignantImpl;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author anwarend
 */
public class ProfilServlet extends HttpServlet {

   
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter(); //on récupère un objet permettant d'écrire dans la future page HTML
        DAOEnseignantImpl en=new DAOEnseignantImpl();
        Enseignant ens=en.find(1);
        out.println("Enseignant 2"+ens.getNom());
        request.setAttribute("enseignant", ens);
        // On re-route l'appel vers la page d'accueil JSP
        //gotoPage("/profil.jsp", request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        if(request.getParameter("supprimer")!=null)
        {
           try {
                DBLink db = new DBLink();
                db.connexion();
                String rq = "DELETE FROM INDISPONIBILITES WHERE NUMINDISPONIBILITE=?";
                PreparedStatement pst = db.getConnection().prepareStatement(rq);
                //pst.setInt(1, Integer.parseInt(request.getParameter("numindisponibilite")));
                //pst.setInt(1,15);
                //db.setPst(pst);
                pst.executeUpdate();
                db.getConnection().close();
            } catch (SQLException ex) {
                Logger.getLogger(ProfilServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if(request.getParameter("valider").equals("valider"))
        {
            try {
                DBLink db = new DBLink();
                db.connexion();
                PreparedStatement pst = db.getConnection().prepareStatement("INSERT INTO INDISPONIBILITES (HORAIRE, JOURS, DEGRE, NUMENSEIGNANT) VALUES (?, ?, ?, ?)");
                pst.setInt(1, Integer.parseInt(request.getParameter("horaire")));
                pst.setInt(2, Integer.parseInt(request.getParameter("jours")));
                pst.setInt(3, Integer.parseInt(request.getParameter("degre")));
                pst.setInt(4, 1);/*SESSION LOGIN*/
                db.setPst(pst);
                db.getPst().executeQuery();
                db.getConnection().close();
            } catch (SQLException ex) {
                Logger.getLogger(ProfilServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        // On re-route l'appel vers la page d'accueil JSP
        gotoPage("/fiche_voeux.jsp", request, response);
        
    }


     private void gotoPage(String adress, HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(adress);
    dispatcher.forward(request,response);
  }
}
